/**
 * <p>Copyright (c) 2011 深圳市鹏途信息技术有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	东莞交通工程质量监督综合业务系统        </p>
 * <p>创建者   :	liuyang 
 * 
 * <p>描   述  :   ReceivedAction.java for com.pengtu.web.admin    </p>
 * 
 * <p>最后修改 : $: 2011-7-28-上午10:27:06 v 1.0.0	 liuyang   $     </p>
 * 
 */

package com.pengtu.web.admin;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.jbpm.pvm.internal.task.TaskImpl;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.Constants;
import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.admin.Received;
import com.pengtu.entity.app.Attachment;
import com.pengtu.entity.app.User;
import com.pengtu.entity.history.HistoryProject;
import com.pengtu.security.SpringSecurityUtils;
import com.pengtu.service.FProjectManager;
import com.pengtu.service.admin.ReceivedManager;
import com.pengtu.service.app.SystemManager;
import com.pengtu.service.history.HistoryProjectManager;
import com.pengtu.utils.web.ControlHelpUtil;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

/**
 * 
 * ReceivedAction
 * 
 * 2011-7-28 上午10:27:06
 * 
 * @version 1.0.0
 * 
 */
@Results({
		@Result(name = Constants.START, location = "${ctx}/admin/received!submit.action?id=${id}&taskId=${taskId}", type = "redirect"),
		@Result(name = Constants.END, location = "${ctx}/admin/received!info.action", type = "redirect"),
		@Result(name = Constants.OVER, location = "${ctx}/admin/received!info.action", type = "redirect"),
		@Result(name = BaseActionSupport.RELOAD, location = "${ctx}/admin/received!page.action?id=${id}", type = "redirect"),
		@Result(name = "part0", location = "${ctx}/admin/received!start.action?id=${id}", type = "redirect"),
		@Result(name = "part1", location = "${ctx}/admin/received!page.action?id=${id}&taskid=${taskid}", type = "redirect"),
		@Result(name = "part2", location = "${ctx}/admin/received!leader.action?id=${id}&taskid=${taskid}", type = "redirect"),
		@Result(name = "part3", location = "${ctx}/admin/received!find.action?id=${id}&taskid=${taskid}", type = "redirect"),
		@Result(name = "part4", location = "${ctx}/admin/received!view.action?id=${id}&taskid=${taskid}", type = "redirect"),
		@Result(name = "part5", location = "${ctx}/admin/received!look.action?id=${id}&taskid=${taskid}", type = "redirect"),
		@Result(name = "part6", location = "${ctx}/admin/received!issue.action?id=${id}&taskid=${taskid}", type = "redirect"),
		@Result(name = Constants.OVER, location = "${ctx}/admin/received!taketask.action?id=${id}&taskid=${taskid}&status=${status}", type = "redirect"),
		@Result(name = Constants.OWN, location = "${ctx}/admin/received/history.action?key=${id}&pngFileName=received.png&acc=1-1&nav=0", type = "redirect") })
public class ReceivedAction extends BaseActionSupport<Received> {

	/**
	 * serialVersionUID:TODO
	 * 
	 * @since 1.0.0
	 */

	private static final long serialVersionUID = 1L;

	private String id;
	private Received entity;
	private String taskid;
	private String status;

	private List<File> upload;
	private List<String> uploadFileName;
	private List<String> uploadContentType;
	private User user = SpringSecurityUtils.getCurrentUser();
	private Page<Received> page = new Page<Received>(10);

	private ReceivedManager receivedManager;
	private SystemManager systemManager;
	private FProjectManager fProjectManager;
	private HistoryProjectManager historyProjectManager;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTaskid() {
		return taskid;
	}

	public void setTaskid(String taskid) {
		this.taskid = taskid;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	/**
	 * upload
	 * 
	 * @return the upload
	 * @since 1.0.0
	 */

	public List<File> getUpload() {
		return upload;
	}

	/**
	 * @param upload
	 *            the upload to set
	 */
	public void setUpload(List<File> upload) {
		this.upload = upload;
	}

	/**
	 * uploadFileName
	 * 
	 * @return the uploadFileName
	 * @since 1.0.0
	 */

	public List<String> getUploadFileName() {
		return uploadFileName;
	}

	/**
	 * @param uploadFileName
	 *            the uploadFileName to set
	 */
	public void setUploadFileName(List<String> uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	/**
	 * uploadContentType
	 * 
	 * @return the uploadContentType
	 * @since 1.0.0
	 */

	public List<String> getUploadContentType() {
		return uploadContentType;
	}

	/**
	 * @param uploadContentType
	 *            the uploadContentType to set
	 */
	public void setUploadContentType(List<String> uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public Page<Received> getPage() {
		return page;
	}

	public void setPage(Page<Received> page) {
		this.page = page;
	}

	@Autowired
	public void setReceivedManager(ReceivedManager receivedManager) {
		this.receivedManager = receivedManager;
	}

	@Autowired
	public void setSystemManager(SystemManager systemManager) {
		this.systemManager = systemManager;
	}

	@Autowired
	public void setfProjectManager(FProjectManager fProjectManager) {
		this.fProjectManager = fProjectManager;
	}

	@Autowired
	public void setHistoryProjectManager(
			HistoryProjectManager historyProjectManager) {
		this.historyProjectManager = historyProjectManager;
	}

	public Received getModel() {
		return entity;
	}

	public String list() throws Exception {
		List<Received> receiveds = receivedManager.find(user);
		List<Received> list = new ArrayList<Received>();
		if (StringUtils.isEmpty(status)) {
			list = receiveds;
		} else {
			for (Received received : receiveds) {
				if (received.getStatus().equals(status)) {
					list.add(received);
				}
			}
		}
		ControlHelpUtil.setObjectToRequest("receiveds", list);
		return SUCCESS;
	}

	/**
	 * 分页查询所有收文 info: 适用:
	 * 
	 * @return
	 * @throws Exception
	 * @exception
	 * @since 1.0.0
	 */
	public String info() throws Exception {
		QueryFilter queryfilter = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());
		if (!page.isOrderBySetted()) {
			page.setOrderBy("createDate");
			page.setOrder(Page.DESC);
		}
		page = receivedManager.findPage(page, queryfilter);
		return Constants.INFO;
	}

	public String input() throws Exception {
		return INPUT;
	}

	/**
	 * 查看页面 last: 适用:
	 * 
	 * @return
	 * @throws Exception
	 * @exception
	 * @since 1.0.0
	 */
	public String last() throws Exception {
		Struts2Utils.getRequest().setAttribute("flag",
				Struts2Utils.getParameter("flag"));
		return Constants.LAST;
	}

	public void prepareLast() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			entity = receivedManager.get(id);
		} else {
			entity = new Received();
		}
	}

	/**
	 * 判断RefeNum是否重复 checkRefeNum: 适用:
	 * 
	 * @return
	 * @throws Exception
	 * @exception
	 * @since 1.0.0
	 */
	public String checkRefeNum() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String newRefeNum = request.getParameter("refeNum");
		String oldRefeNum = request.getParameter("oldRefeNum");
		if (receivedManager.isRefeNumUnique(newRefeNum, oldRefeNum)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
		return null;
	}

	/**
	 * 检查收文标题是否唯一 checkTitle: 适用:
	 * 
	 * @return
	 * @throws Exception
	 * @exception
	 * @since 1.0.0
	 */
	public String checkTitle() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String newTitle = request.getParameter("title");
		String oldTitle = request.getParameter("oldTitle");
		if (receivedManager.isTitleUnique(newTitle, oldTitle)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
		return null;
	}

	/**
	 * 启动流程 start: 适用:
	 * 
	 * @return
	 * @throws Exception
	 * @exception
	 * @since 1.0.0
	 */
	public String start() throws Exception {
		entity.setStatus("02");
		String deploymentId = fProjectManager.getByKey("received")
				.getDeploymentId();
		receivedManager.start(entity, deploymentId, user);
		return Constants.END;
	}

	public void prepareStart() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			entity = receivedManager.get(id);
		} else {
			entity = new Received();
		}
	}

	/**
	 * 保存 second: 适用:
	 * 
	 * @return
	 * @throws Exception
	 * @exception
	 * @since 1.0.0
	 */
	public String second() throws Exception {
		entity.setStatus("01");
		if (StringUtils.isEmpty(id)) {
			receivedManager.save(entity);
		}
		String rootPath = Struts2Utils.getAppRootPath();
		if (null != upload) {
			for (int i = 0; i < upload.size(); i++) {
				Attachment attachmention = new Attachment();
				entity.setAcctPetitionFile(uploadFileName.get(i));
				attachmention.setUpfile(upload.get(i));
				attachmention.setUpfileFileName(uploadFileName.get(i));
				attachmention.setType(uploadContentType.get(i));
				attachmention.setTableId(entity.getId());
				attachmention.setTableField("receivedFile");
				attachmention.setTableName("Received");
				attachmention.setType("收文拟稿");
				attachmention.setFileTime(new Date());
				attachmention.setPartName(user.getUserName());
				systemManager.uploadFile(rootPath, attachmention);
			}
		}
		receivedManager.save(entity);
		return "part0";
	}

	public void prepareSecond() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			entity = receivedManager.get(id);
		} else {
			entity = new Received();
		}
	}

	public String save() throws Exception {
		if (StringUtils.isEmpty(id)) {
			entity.setStatus("01");
			receivedManager.save(entity);
		}
		String rootPath = Struts2Utils.getAppRootPath();
		if (null != upload) {
			for (int i = 0; i < upload.size(); i++) {
				Attachment attachmention = new Attachment();
				entity.setAcctPetitionFile(uploadFileName.get(i));
				attachmention.setUpfile(upload.get(i));
				attachmention.setUpfileFileName(uploadFileName.get(i));
				attachmention.setType(uploadContentType.get(i));
				attachmention.setTableId(entity.getId());
				attachmention.setTableField("receivedFile");
				attachmention.setTableName("Received");
				attachmention.setType("收文拟稿");
				attachmention.setFileTime(new Date());
				attachmention.setPartName(user.getUserName());
				systemManager.uploadFile(rootPath, attachmention);
			}
		}
		receivedManager.save(entity);
		return Constants.END;
	}

	/**
	 * 删除
	 */
	public String delete() throws Exception {
		receivedManager.delete(entity);
		addActionMessage("删除成功！");
		return Constants.END;
	}

	public void prepareDelete() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			entity = receivedManager.get(id);
		} else {
			entity = new Received();
		}
	}

	protected void prepareModel() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			entity = receivedManager.get(id);
		} else {
			entity = new Received();
		}
	}

	/**
	 * 判断是否待办 judge: 适用:
	 * 
	 * @return
	 * @throws Exception
	 * @exception
	 * @since 1.0.0
	 */
	public String judge() throws Exception {
		taskid = receivedManager.isPersonalTasks(user, id);
		if (StringUtils.isEmpty(status)) {
			status = entity.getStatus();
		}
		if (StringUtils.isNotEmpty(taskid)) {
			Struts2Utils.getSession().setAttribute("taskid", taskid);
			return Constants.OVER;
		}
		return Constants.OWN;
	}

	public void prepareJudge() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			entity = receivedManager.get(id);
		} else {
			entity = new Received();
		}
	}

	/**
	 * 接受任务 taketask: 适用:
	 * 
	 * @return
	 * @throws Exception
	 * @exception
	 * @since 1.0.0
	 */
	public String taketask() throws Exception {
		if (StringUtils.isEmpty(taskid)) {
			taskid = ControlHelpUtil.getTaskId();
		}
		HistoryProject historyProject = receivedManager
				.getHistoryProject(taskid);
		String activityName = receivedManager.getTask(taskid).getActivityName();
		if (historyProject == null) {
			historyProject = new HistoryProject();
			historyProject.setUserName(user.getUserName());
			historyProject.setKey(id);
			historyProject.setStatus("001" + status);
			historyProject.setStartDate(new Date());
			historyProject.setTaskId(taskid);
			historyProject.setActivityName(activityName);
		}
		try {
			receivedManager.takeTask(taskid, id, user, historyProject);
		} catch (org.jbpm.api.JbpmException e) {
			receivedManager.save(historyProject);
		}
		if (StringUtils.equals(activityName, "拟办意见")) {
			return "part1";
		} else if (StringUtils.equals(activityName, "主办部门")) {
			TaskImpl taskImpl = (TaskImpl) receivedManager.getTask(taskid);
			String master = (String) taskImpl.getVariable("master");
			if (StringUtils.equals(user.getUserName(), master)) {
				return "part3";
			} else {
				return "part2";
			}
		} else if (StringUtils.equals(activityName, "经办人")) {
			return "part4";
		} else if (StringUtils.equals(activityName, "部门负责人")) {
			return "part5";
		} else if (StringUtils.equals(activityName, "归档")) {
			return "part6";
		}
		return Constants.OWN;
	}

	public void prepareTaketask() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			entity = receivedManager.get(id);
		} else {
			entity = new Received();
		}
	}

	/**
	 * 页面 page: 适用:
	 * 
	 * @return
	 * @throws Exception
	 * @exception
	 * @since 1.0.0
	 */
	public String page() throws Exception {
		return Constants.PAGE;
	}

	public void preparePage() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			entity = receivedManager.get(id);
		} else {
			entity = new Received();
		}
	}
	
	/**
	 * 页面 page: 适用:
	 * 
	 * @return
	 * @throws Exception
	 * @exception
	 * @since 1.0.0
	 */
	public String leader() throws Exception {
		return Constants.LEADER;
	}

	public void prepareLeader() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			entity = receivedManager.get(id);
		} else {
			entity = new Received();
		}
	}
	
	/**
	 * 页面 page: 适用:
	 * 
	 * @return
	 * @throws Exception
	 * @exception
	 * @since 1.0.0
	 */
	public String find() throws Exception {
		return Constants.FIND;
	}

	public void prepareFind() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			entity = receivedManager.get(id);
		} else {
			entity = new Received();
		}
	}
	
	/**
	 * 经办人反馈结果
	 * view:
	 * 适用:
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	public String view() throws Exception {
		return VIEW;
	}

	public void prepareView() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			entity = receivedManager.get(id);
		} else {
			entity = new Received();
		}
	}
	
	/**
	 * 部门负责人审核
	 * look:
	 * 适用:
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	public String look() throws Exception {
		return Constants.LOOK;
	}

	public void prepareLook() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			entity = receivedManager.get(id);
		} else {
			entity = new Received();
		}
	}
	
	/**
	 * 归档
	 * issue:
	 * 适用:
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	public String issue() throws Exception {
		return Constants.ISSUE;
	}

	public void prepareIssue() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			entity = receivedManager.get(id);
		} else {
			entity = new Received();
		}
	}

	/**
	 * 流程提交 save1: 适用:
	 * 
	 * @return
	 * @throws Exception
	 * @exception
	 * @since 1.0.0
	 */
	public String save1() throws Exception {
		String key = Struts2Utils.getParameter("key");
		String key1 = Struts2Utils.getParameter("key1");
		String key2 = Struts2Utils.getParameter("key2");
		String key3 = Struts2Utils.getParameter("key3");
		String value = Struts2Utils.getParameter("value");
		String value1 = Struts2Utils.getParameter("value1");
		String value2 = Struts2Utils.getParameter("value2");
		String value3 = Struts2Utils.getParameter("value3");
		String outcome = Struts2Utils.getParameter("outcome");
		String view = Struts2Utils.getParameter("view");
		String usernames = null;
		Map<String, Object> variables = new HashMap<String, Object>();
		if (StringUtils.isNotEmpty(key)) {
			variables.put(key, value);
			if (StringUtils.isNotEmpty(usernames)) {
				usernames = usernames + "," + value;
			} else {
				usernames = value;
			}
		}
		if (StringUtils.isNotEmpty(key1)) {
			if (StringUtils.isNotEmpty(usernames)) {
				usernames = usernames + "," + value1;
			} else {
				usernames = value1;
			}
		}
		if (StringUtils.isNotEmpty(key2)) {
			if (StringUtils.isNotEmpty(usernames)) {
				usernames = usernames + "," + value2;
			} else {
				usernames = value2;
			}
		}
		if (StringUtils.isNotEmpty(key3)) {
			variables.put(key3, value3);
		}
		if (StringUtils.isNotEmpty(usernames)) {
			variables.put("usernames", usernames);
			variables.put("quorum", usernames.split(",").length);
			System.out.println("=============="+usernames.split(",").length);
		}
		HistoryProject historyProject = receivedManager.getHistoryProject(taskid);
		historyProject.setEndDate(new Date());
		historyProject.setMessage(user.getName()+":"+view);
		receivedManager.completeTask(taskid, outcome, variables);
		receivedManager.save(entity);
		historyProjectManager.save(historyProject);
		return Constants.END;
	}

	public void prepareSave1() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			entity = receivedManager.get(id);
		} else {
			entity = new Received();
		}
	}
}
